/*
 *
 *  Copyright (C) 2009 UMA
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Juan Pablo Mar�n Tobar <jpablomt@uma.es>
 *
 *  Version: 1.0
 */

package com.LibreGeoSocial.App.Perfil;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class QuestionHandler extends DefaultHandler {
	
	public static final String ERROR = "error";
	
	/*
	 * three object parse:
	 *  - Question
	 *  - Answer 
	 */
	private final static int PARSE_NULL = 0;
	private final static int PARSE_QUESTION = 1;
	private final static int PARSE_ANSWER = 2;
	private final static int PARSE_ERROR = 3;
	
	/*
	 * which type object is parse
	 */
	private int objectParse;
	
	/*
	 * parse state	
	 */
	private int stateParse;
	/*
	 * state of parse
	 */
	private final static int TRANSIT = -1;
	private final static int INITIAL = 0;
	private final static int LGS = 1;
	private final static int QUESTION = 2;
	private final static int PROPOSAL = 3;
	private final static int QDESCRIPTOR = 4;
	private final static int END = 5;
	
	/*
	 * local variables for make and return question, answer or error
	 */
	private Question objectQuestion;
	private AnswerServer objectAnswer;
	private Answer answer;

	private boolean in_type;
	private boolean in_code;
	private boolean in_result;
	private boolean in_description;
	private boolean in_message;
	private boolean in_text;
	private boolean in_topic;
	private boolean in_identifier;
	
	private String TAG = "handler";

	@Override
	public void startDocument() throws SAXException {
		objectParse = PARSE_NULL;
		stateParse = LGS;
		in_type = false;
	}

	@Override
	public void endDocument() throws SAXException {
		
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		if (localName.equals(ProfileInformation.TYPE)) in_type = true;
		else if (localName.equals(ProfileInformation.QUESTION)) stateParse = QUESTION;
		else if (localName.equals(ProfileInformation.IDENTIFIER)) in_identifier = true;
		else if (localName.equals(ProfileInformation.TOPIC)) in_topic = true;
		else if (localName.equals(ProfileInformation.TEXT)) in_text = true;
		else if (localName.equals(ProfileInformation.PROPOSAL)) stateParse = PROPOSAL;
		else if (localName.equals(ProfileInformation.ANSWER) && (objectParse==PARSE_QUESTION))
			answer = new Answer();
		else if (localName.equals(ProfileInformation.CODE)) in_code = true;
		else if (localName.equals(ProfileInformation.QDESCRIPTOR)) stateParse = QDESCRIPTOR;
		else if (localName.equals(ProfileInformation.DESCRIPTION)) in_description = true;
		else if (localName.equals(ProfileInformation.MESSAGE)) in_message = true;
		else if (localName.equals(ProfileInformation.RESULT)) in_result = true;
		
	}
	
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		String s = new String(ch, start, length);
		if ((stateParse==LGS) && (in_type)){
			if (s.equals(ProfileInformation.QUESTION)){
				objectParse = PARSE_QUESTION;
				objectQuestion = new Question();
				objectQuestion.setStatus(Question.RIGTH);
			}
			else if (s.equals(ProfileInformation.ANSWER)){
				objectParse = PARSE_ANSWER;
				objectAnswer = new AnswerServer();
			}
			else if (s.equals(ERROR)){
				objectParse = PARSE_ERROR;
				objectQuestion = new Question();
				objectQuestion.setStatus(Question.ERROR);
			}
		} else
			switch (objectParse){
			case PARSE_QUESTION:
				switch(stateParse){
				case QDESCRIPTOR:
					if (in_code) objectQuestion.setCode(Integer.parseInt(s));
					break;
				case QUESTION:
					if (in_identifier) objectQuestion.setIdentifier(Integer.parseInt(s));
					else if (in_topic) objectQuestion.setTopic(s);
					else if (in_type) objectQuestion.setType(s);
					else if (in_text) objectQuestion.setText(s);
					break;
				case PROPOSAL:
					if (in_code) answer.setCode(Integer.parseInt(s));
					else if (in_text) answer.setText(s);
					break;
				}
				break;
			case PARSE_ANSWER:
				if (in_code) objectAnswer.setCode(Integer.parseInt(s));
				else if (in_result) objectAnswer.setResult(s);
				else if (in_message) objectAnswer.setMessage(s);
				break;
			case PARSE_ERROR:
				if (in_description) objectQuestion.setErrorDescription(s);
				if (in_code) objectQuestion.setCode(Integer.parseInt(s));
				break;
		}
	}
	
	
	
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException{
		if (localName.equals(ProfileInformation.LGS)) stateParse = END;
		else if (localName.equals(ProfileInformation.QUESTION)) stateParse = TRANSIT;
		else if (localName.equals(ProfileInformation.IDENTIFIER)) in_identifier = false;
		else if (localName.equals(ProfileInformation.TOPIC)) in_topic = false;
		else if (localName.equals(ProfileInformation.TEXT)) in_text = false;
		else if (localName.equals(ProfileInformation.TYPE)) in_type = false;
		else if (localName.equals(ProfileInformation.ANSWER) && (objectParse==PARSE_QUESTION)) objectQuestion.addAnswer(answer);
		else if (localName.equals(ProfileInformation.CODE)) in_code = false;
		else if (localName.equals(ProfileInformation.DESCRIPTION)) in_description = false;
		else if (localName.equals(ProfileInformation.RESULT)) in_result = false;
		else if (localName.equals(ProfileInformation.MESSAGE)) in_message = false;
		else if (localName.equals(ProfileInformation.PROPOSAL)) stateParse = TRANSIT;
	}


	public Question getParseQuestion(){
		return objectQuestion;
	}
	
	public AnswerServer getAnswerServer(){
		return objectAnswer;
	}
	
}